package com.chris.core.socket.bio.contorller;

import com.chris.core.socket.bio.service.SokcetBioClientService;
import com.chris.core.socket.bio.service.SokcetBioServerService;
import com.chris.core.socket.bio.util.BioSokcetClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.io.*;

@RestController
@RequestMapping(value = "/api")
public class testContorller {


    @Autowired
    SokcetBioServerService sokcetBioServerService;

    @Autowired
    SokcetBioClientService sokcetBioClientService;
   // ConcurrentHashMap<String, BioSokcetClientUtil> list = new ConcurrentHashMap<>();

    @RequestMapping(value = "stop", method = RequestMethod.GET)
    public String stop(@PathParam("sid") String sid)
    {
        sokcetBioServerService.sokcet.Stop(sid);
        return "删除:"+sid;
    }

    @RequestMapping(value = "clintsingle", method = RequestMethod.GET)
    public String clintsingle() throws IOException {
        SokcetBioClientService service = new SokcetBioClientService();
       // service.s
        return "clintsingle";
    }

    /**
     * 发送信息
     * @param msg
     * @return
     */
    @RequestMapping(value = "csend", method = RequestMethod.GET)
    public String csend(@PathParam("msg") String msg)
    {
        sokcetBioClientService.sokcet.SendMsg("\n2222222\n111111\n\nbbq8\n123456\n");
        return "csend";
    }

    /**
     * 发送信息
     * @param msg
     * @param sid
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "ssend", method = RequestMethod.GET)
    public String ssend(@PathParam("msg") String msg,@PathParam("sid") String sid) throws IOException {
        sokcetBioServerService.sokcet.SendClientMsg("我msg1111\n32324234\nmalsdfasdf\nmsg1111\n32324234\nmalsdfasdf\n",sid);
        return "csend";
    }


    @RequestMapping(value = "upload", method = RequestMethod.GET)
    public String upload() throws IOException {
        File file = new File("./tip.log");
        if (file != null && file.isFile() && file.exists()) {
           // InputStream inputStream = new FileInputStream(file);
            sokcetBioClientService.sokcet.SendFile("c_"+file.getName(),file);
        }
        return "upload";
    }

    @RequestMapping(value = "supload", method = RequestMethod.GET)
    public String supload(@PathParam("sid") String sid) throws IOException {
        File file = new File("./tip.log");
        if (file != null && file.isFile() && file.exists()) {
            // InputStream inputStream = new FileInputStream(file);
            sokcetBioServerService.sokcet.SendClientMsg("s_"+file.getName(),file,sid);
        }
        return "upload";
    }



    /**
     * 并发测试连接
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "test_contenct", method = RequestMethod.GET)
    public String test_contenct() throws IOException, InterruptedException {
        for (int i=0;i<10000;i++)
        {

            BioSokcetClientUtil aa = new BioSokcetClientUtil("127.0.0.1", 10101,300000, false, "!@#$bbq8%^&*") {
                @Override
                public void OnRevice(String mag) {

                }
            };

            Thread.sleep(1);
           // list.put(System.currentTimeMillis()+"",aa);
        }
        return "test_contenct";
    }



    /**
     * 并发测试连接
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "test_t", method = RequestMethod.GET)
    public int test_t() throws IOException {
        return sokcetBioServerService.sokcet.getOnlineCount();
    }

    /**
     * 垃圾回收
     * @return
     */
    @RequestMapping(value = "gc", method = RequestMethod.GET)
    public String gc()
    {
        System.gc();
        return "gc";
            }

    /**
     * 并发测试连接
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "ccsend", method = RequestMethod.GET)
    public String ccsend() throws IOException {
         sokcetBioServerService.sokcet.SendAllClientMsg("hello all cilint");
        return "ccsend";
    }

}
